
--注册常规合成
register_crad_rule('葫芦小金刚',{'爷爷','大娃','二娃','三娃','四娃','五娃','六娃','七娃'})


--入口
local mt = ac.skill['爷爷']
local crads = {
    '爷爷',
    '穿山甲',
    '蝙蝠精',
    '蜥蜴精',
    '壁虎精',
    '蜘蛛精',
    '蛤蟆精',
    '蜈蚣精',
    '大娃',
    '二娃',
    '三娃',
    '四娃',
    '五娃',
    '六娃',
    '七娃',
    '蝎子精',
}

function mt:on_card(hero,player)
    if hero[self.name] then
        return
    end
    hero[self.name] = true
    for _,name in ipairs(crads) do
        player:set_data(name,true)
    end

    player:event '卡池-添加卡组'(function(_,_,pool)
        for _,name in ipairs(crads) do
            pool[#pool+1] = name
        end
    end)
    hero:event '天赋-检测合成后'(function(trg)
        local haves = hero:get_data('拥有天赋表') or {}
        for _,name in ipairs(crads) do
            if not haves[name] then
                return
            end
        end
        trg:remove()
        hero:set_data('完成-计数',hero:get_data('完成-计数',0) + 1)
    end)
end


local list = {
    ['大娃'] = true,
    ['二娃'] = true,
    ['三娃'] = true,
    ['四娃'] = true,
    ['五娃'] = true,
    ['六娃'] = true,
    ['七娃'] = true,
}
ac.game:event '英雄-抽卡完毕'(function(_,hero,temp)
    if list[temp.name] then
        temp.show_num = 400
        temp:gc(hero:event '单位-击杀'(function()
            temp.show_num  = temp.show_num - 1
            if temp.show_num<=0 then
                hide_card(temp)
            end
        end))
    end
end)

local mt = ac.skill['蝎子精']
function mt:on_card(hero,player)
    self:gc(hero:event '天赋-检测合成'(function(trg)
        local count = hero:get_data('精怪-计数',0)
        if count>=8 then
            for _,temp in ipairs(get_card_list(hero,'精怪')) do
                hide_card(temp)
            end
            trg:remove()
            ac.wait(10,function()
                hero:notify('英雄-获得天赋',hero,'蛇精')
            end)
        end
    end))
end

local mt = ac.skill['力大无穷']
mt.temp = '攻击计数'
mt.count = 14
mt.model1 = [[d6988d8e572494b4bb5585894ec89d60.mdl]]
function mt:on_add()
    self.count = self.data1
end
function mt:on_run(hero,target)
    local hero = self.owner
    local damage = hero:get(self['属性1']) * self.data2 / 100
    hero:add_effect(self.model1):remove()
    hero:aoe_damage(target,600,damage,self)
end


local mt = ac.skill['激光眼']
mt.temp = '攻击触发'
mt.missile_art = [[e341d2766297cae0a341dad10b818517.mdl]]

function mt:on_init()
    self.cool = self.data1
end
function mt:on_run(hero,target)
    local hero = self.owner
    local damage = hero:get(self['属性1']) * self.data2 / 100
    local lock = {}
    local angle = hero:get_point() / target:get_point()
    local num = 4
    for a=1,num do
        local r = angle - (a-num/2 -0.5)*15
        ac.wait((a-1)*50,function()
            local mover = hero:launch_wave(self.missile_art,r,1200,1600,nil,50)
            sc.loopUnit(0.03,mover,275,function(_,unit)
                if unit:is_enemy(hero) then
                    hero:create_damage(unit,damage,self)
                end
            end):set_lock(lock)
        end)
    end
end

local mt = ac.skill['钢筋铁骨']

function mt:on_add()
    local hero = self.owner
    self:gc(ac.loop(200,function()
        if hero:get('当前生命百分比')<self.data1 then
            if self.attr==nil then
                self.attr = hero:add_buff(self.name){
                    art = self.art,
                    tip = self:get_tip(),
                    keep = true,
                    on_add = function(buff)
                        buff:gc(hero:add(self['属性2'],self.data2))
                    end
                }
            end
        else
            if self.attr then
                self.attr:remove()
                self.attr = nil
            end
        end
    end))
    self:gc(function()
        if self.attr then
            self.attr:remove()
            self.attr = nil
        end
    end)
end


local mt = ac.skill['烈焰-烈焰']
mt.temp = '冲击波'
mt.missile_art = ''
mt.start_area = 50
mt.final_area = 600

local mt = ac.skill['烈焰']

function mt:on_add()
    local hero = self.owner
    self.cool = self.data1
    self.temp_skill = hero:add_skill('烈焰-烈焰','隐藏')
    self:gc(ac.loop(self.data1*1000,function()
        if not hero:is_alive() then
            return
        end
        local damage = hero:get(self['属性1']) * self.data2 / 100
        local angle = hero:get_facing()
        local point = hero:get_point()
        point:effect{
            model = [[4c8c586b45f6563e9a2f70349a815172.mdl]],
            angle = angle,
            time = 1,
        }
        self.temp_skill.damage = damage
        local cast = self.temp_skill:create_cast()
        cast.damage = damage
        cast:shot_missile(angle)
    end))
end



local mt = ac.skill['水疗']
mt.model1 = [[d79b6e36b5388413f69854db3a90e320.mdl]]
function mt:on_add()
    local hero = self.owner
    self:gc(ac.loop(self.data1*1000,function()
        hero:add_effect(self.model1):remove()
        hero:add(self['属性2'],hero:get(self['属性1']) * self.data2/100)
    end))
end


local mt = ac.skill['无影无形']
mt.temp = '攻击触发'
mt.odds = 25
function mt:on_init()
    self.odds = self.data1
end

function mt:on_run(hero,target)
    local hero = self.owner
    local damage = hero:get(self['属性1']) * self.data2 / 100
    target:add_effect([[483fd5c36929e89f3fb051fa06a54a03.mdl]],'origin'):remove()
    target:damage{
        source = hero,
        damage = damage,
        skill = self,
    }
end


local mt = ac.skill['乾坤葫芦']

function mt:on_add()
    local hero = self.owner
    self:gc(hero:event '单位-拦截受到伤害'(function(_,_,target,data)
        if odd_run(self.data1) then
            return true
        end
    end))
end



local mt = ac.skill['大聪明']

function mt:on_add(hero,target)
    local hero = self.owner
    self:gc(hero:event '单位-攻击命中'(function()
        hero:add_buff(self.name){
            art = self.art,
            tip = self:get_tip(),
            keep = true,
            time = self.data3,
            stack = 0,
            on_add = function(buff)
                buff.stack = buff.stack + 1
                buff:gc(hero:add(self['属性1'],self.data1))
            end,
            on_cover = function(buff)
                if buff.stack<self.data2 then
                    buff:on_add()
                end
                buff:set_remaining(buff.time)
            end
        }
    end))
end

local mt = ac.skill['我滴任务完成啦']

function mt:on_add(hero,target)
    local hero = self.owner
    self:gc(hero:event '单位-释放技能结束'(function(_,_,skill)
        if skill.is_order then
            return
        end
        hero:add_buff(self.name){
            art = self.art,
            tip = self:get_tip(),
            keep = true,
            time = self.data2,
            stack = 0,
            on_add = function(buff)
                buff.stack = buff.stack + 1
                buff:gc(hero:add(self['属性1'],self.data1))
            end,
            on_cover = function(buff)
                if buff.stack<10 then
                    buff:on_add()
                end
                buff:set_remaining(buff.time)
            end
        }
    end))
end


local mt = ac.skill['毒钩']

function mt:on_add()
    local hero = self.owner
    self:gc(hero:event '单位-造成伤害前'(function(_,_,target,data)
        if data.skill and data['附加']==nil then
            target:add_buff '蝎子精-毒钩'{skill = self,source = hero,time = self.data2}
        end
    end))
end


local buff = ac.buff['蝎子精-毒钩']
buff.time = 3
buff.pulse = 1
buff.effect_data = {
    ['origin'] = [[Abilities\Weapons\PoisonSting\PoisonStingTarget.mdl]]
}
function buff:on_cover()
    self:set_remaining(self.time)
    return false
end

function buff:on_pulse()
    local skill = self.skill
    local source = self.source
    local damage = source:get(skill['属性2']) * skill.data1 / 100
    self.owner:damage{
        source = source,
        damage = damage,
        skill = self,
        ['附加'] = true,
    }
end


local mt = ac.skill['万毒妖雾']
mt.temp = '击杀单位'

function mt:on_run(hero,target)
    local hero = self.owner
    local point = target:get_point()
    hero:add_buff '光环'{
        time = self.data2,
        buff_name = '蛇精-毒雾',
        buff_data = {skill = self,source = hero},
        selector = ac.selector('unit'):range(point,300):enemy(hero),
    }
end


local buff = ac.buff['蛇精-毒雾']
buff.cover_type = 1
buff.cover_max = 1
buff.pulse = 1

function buff:on_pulse()
    local skill = self.skill
    local target = self.target
    local source = self.source
    local damage = source:get(skill['属性1']) * skill.data1 / 100
    target:damage{
        source = hero,
        damage = damage,
        skill = skill,
    }
end


local mt = ac.skill['如意']
mt.temp = '攻击触发'
function mt:on_run()
    local hero = self.owner
    hero:add_buff(self.name){
        art = self.art,
        tip = self:get_tip(),
        keep = true,
        time = self.data1,
        on_add = function(buff)
            buff:gc(hero:add(self['属性1'],self.data2))
        end,
    }
end

local tips = {
    '|cffff0000红娃神力|r',
    '|cffff9900橙娃洞悉|r',
    '|cffffff00黄娃钢拳|r',
    '|cff00ff00绿娃烈焰|r',
    '|cff33cccc青娃怒涛|r',
    '|cff00ccff蓝娃隐袭|r',
    '|cffff00ff紫娃宝葫|r',
}

local mt = ac.skill['七色彩光']
mt.model1 = [[d6988d8e572494b4bb5585894ec89d60.mdl]]
mt.model2 = [[33be26625aa25e6fbd76a6518fb07182.mdl]]
function mt:on_add()
    local hero = self.owner
    local count = 0
    self:gc(hero:add_buff '七色彩光'{
        art = self.art,
        tip1 = self:get_tip(),
        keep = true,
        pulse = 10,
        on_add = function(buff)
            buff:on_pulse()
        end,
        on_pulse = function(buff)
            count = math.random(7)
            buff.tip = ('%s|n|n当前：%s'):format(buff.tip1,tips[count])
            if count==2 then
                for u in ac.selector('unit')
                    : range(hero,1000)
                    : enemy(hero)
                    : loop()
                do
                    if u:is_type('精英') then
                        u:add_buff '葫芦小金刚-标记'{time = 3}
                    end
                end
            end
            if count==3 then
                local attr = hero:add('护甲穿透',self.data3)
                ac.wait(5*1000,function()
                    attr()
                end)
            end
            if count==6 then
                local target = ac.selector('unit'):range(hero,3000):enemy(hero):sorter(function(a,b)
                    return a:get('攻击')>b:get('攻击')
                end):first()
                if target then
                    local angle = math.random(360)
                    local point = target:get_point() - {angle,150}
                    local effect = point:effect{
                        model = hero:get_model(),
                        hide_when_remove = true,
                        angle = angle + 180,
                        speed = 2,
                        time = 0.5,
                    }
                    effect:set_animation('attack')
                    ac.wait(200,function()
                        local damage = hero:get(self['属性1']) * self.data7 / 100
                        target:damage{
                            source = hero,
                            damage = damage,
                            skill = self,
                            ['暴击'] = true,
                        }
                    end)
                end
            end
        end
    })

    self:gc(self.owner:event '单位-造成伤害前'(function(_,_,target,data)
        if target:find_buff('葫芦小金刚-标记') then
            data:mul(self.data2)
        end
    end))

    self:gc(hero:event '单位-攻击命中'(function(_,_,target)
        if count==1 then
            count = 0
            local damage = hero:get(self['属性1']) * self.data1 / 100
            target:get_point():add_effect(self.model1):remove()
            hero:aoe_damage(target,600,damage,self,function(unit)
                unit:add_buff '击飞'{time = 0.5}
            end)
        elseif count==4 then
            target:add_buff '葫芦小金刚-燃烧'{data1 = self.data4}
        elseif count==5 then
            local point= target:get_point()
            count = 0
            local damage = hero:get(self['属性1']) * self.data5/100
            hero:heal(hero:get('生命上限') * self.data6/100)
            target:get_point():add_effect(self.model2):remove()
            hero:aoe_damage(target,600,damage,self,function(unit)
                local angle = point/unit:get_point()
                unit:add_buff '位移'{time = 0.5,angle = angle}
            end)
        end
    end))

    self:gc(hero:event '单位-拦截受到伤害'(function(_,data)
        if count==7 and data.skill then
            count = 0
        end
    end))
end



local buff = ac.buff['葫芦小金刚-燃烧']
buff.time = 3
buff.cover_type = 1
buff.data1 = 0

function buff:on_pulse()
    self:gc(hero.owner:add('额外护甲',-self.data1))
end



local mt = ac.skill['七色神光']
mt.temp = '攻击触发'
mt.model1 = [[7f8f335977d25ebeb70c78c20c093c68.mdl]]
mt.model2 = [[520b52f30d886003c215b2b8aedb74ea.mdl]]
mt.model3 = [[f890d838734a4d4be83cd1343d031177.mdl]]
function mt:on_run(hero,target)
    local damage = hero:get(self['属性1']) * self.data1 / 100 + hero:get(self['属性2']) * self.data2/100
    local lock = {}
    ac.timer(200,self.data3,function(t)
        local unit = ac.selector('unit'):range(hero,900):enemy(hero):random()
        if unit then
            local damage1 = damage
            if lock[unit.handle] then
                damage1 = damage1/2
            end
            lock[unit.handle] = true
            local model1 = table.random({self.model1,self.model2,self.model3})
            
            hero:bezier_missile(unit,{-135,500},model1,1200,function()
                hero:create_damage(unit,damage1,self)
            end)
        end
    end)
end
